using System;
using System.ComponentModel;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Windows.Forms;
using System.Xml; // some comments
using System.Net; // just added to test some diffing
using Microsoft.TeamFoundation.Client;
using Microsoft.TeamFoundation.VersionControl.Client;
using System.Collections.Generic;
using Ltf.Remoting.Settings;
using Ltf.Remoting.RemoteEvents;
using Ltf.Remoting.TeamFoundation;
using System.Threading;
using Ltf.Remoting.Commands;
using Ltf.Remoting.TFAccess;
using Ltf.MonitorLizard.Helpers;
using System.Reflection;
using Ltf.Diffs;
using Ltf.Remoting.Operations;
using System.Collections;
using System.Text;
using Ltf.Diffs.MergeCommands;
using Ltf.MonitorLizard.Controls;
using Ltf.MonitorLizard.Controllers;

namespace Ltf.MonitorLizard.Forms
{
	public partial class DirDiff
	{
		private Workspace workspace1;
		private Workspace workspace2;

		private TFServerConnection connection1;
		private TFServerConnection connection2;


		public void RunDiffer(bool isPatchFileRequired, string[] oldNames, string[] newNames, List<string> includedFiles)
		{
			//		VersionControlServer vcServer = ServerConnections.Connection.VersionControlServer;

			diffPath1 = txtPath1.Text;
			diffPath2 = txtPath2.Text;
			string errMsg = "Could not diff as:\n";
			bool errs = false;

			string targetPath = string.Empty;
			if (rbtLizardMerge.Checked && (isPrepareMerge || isMerge))
			{
				if (Directory.Exists(txtLtfMergeTarget.Text))
					targetPath = txtLtfMergeTarget.Text;
				else
				{
					errMsg += "The target path '" + txtLtfMergeTarget.Text + "' does not exist\n";
					errs = true;
				}
			}

			if (sselConnection1.Connection == null && sselConnection2.Connection != null)
				sselConnection1.Connection = sselConnection2.Connection;
			if (sselConnection2.Connection == null && sselConnection1.Connection != null)
				sselConnection2.Connection = sselConnection1.Connection;

			//TODO: connections should be passed from here.
			IDifRepository repo1 = LizardFileAccess.GetRepositoryForPath(sselConnection1.Connection, diffPath1,
				LizardFileAccess.GetChangesetIdFromVersionText(cmbVersion1.Text), deletionId1);
			IDifRepository repo2;
			if (!chkCompareToLocal.Checked)
				repo2 = LizardFileAccess.GetRepositoryForPath(sselConnection2.Connection, diffPath2,
					LizardFileAccess.GetChangesetIdFromVersionText(cmbVersion2.Text), deletionId2);
			else
			{
				//TODO: something needs to be done if the first path is a server path.
				VersionControlServer vcServer = ServerConnections.GetConnectionForPath(diffPath1).VersionControlServer;
				repo2 = new LocalTFDifRepository(vcServer, ServerConnections.DefaultConnection.Settings.TempPath);
			}
			if (!repo1.FolderExists(diffPath1))
			{
				errMsg += "The path '" + diffPath1 + "' does not exist\n";
				errs = true;
			}
			if (!repo2.FolderExists(diffPath2))
			{
				errMsg += "The path '" + diffPath2 + "' does not exist\n";
				errs = true;
			}
			if (errs)
			{
				MessageBox.Show(errMsg, "Paths not valid", MessageBoxButtons.OK);
				return;
			}
			Differ = new DirDiffer(repo2, repo1, diffPath2, diffPath1, targetPath,
			 ckbIgnoreTabExpansions.Checked, ckbIgnoreSpaceChanges.Checked,
			 ckbIgnoreWhiteSpace.Checked, ckbIgnoreBlankLines.Checked,
			 ckbIgnoreCase.Checked, includedFiles);

			Differ.SetRenameOldNames(oldNames);
			Differ.SetRenameNewNames(newNames);

			repo1.PrepCache(diffPath1);

			repo2.PrepCache(diffPath2);

			this.Cursor = Cursors.WaitCursor;
			int count = repo1.GetQuickItemCount(ServerConnections.GetConnectionForPath(diffPath1).Settings, diffPath1);
			progressIndicator = new ProgressIndicator("Lizard Diff", "Comparing ...", count, 1, true);

			//differ.DiffAnotherTen += new EventHandler(differ_DiffAnotherTen);
			Differ.ProgressClick += new EventHandler(progressIndicator.IncrementHandler);
			Differ.Finished += new EventHandler(differ_Finished);
			ThreadStart ts;
			if (isPatchFileRequired)
				ts = new ThreadStart(Differ.GeneratePatchFile);
			else
				ts = new ThreadStart(Differ.BeginDiffs);
			Thread t = new Thread(ts);
			isComparing = true;
			t.Start();


		}

	}
}
